#!/usr/bin/python
import os

class S7ZipCompressor:
	def __commandTemplate(self, string, command):
		argsDict = {}
		argsDict["infile"] = "/tmp/sometempinfile"+str(os.getpid())
		argsDict["outfile"] = "/tmp/sometempoutfile.7z"+str(os.getpid())
		
		os.system("rm -f %s\n" % argsDict["outfile"])
		open(argsDict["infile"],"w+").write(string)
		os.system(command % argsDict)
		data = open(argsDict["outfile"]).read()
		os.system("rm -f " + argsDict["infile"])
		os.system("rm -f " + argsDict["outfile"])
		return data
	
	def compress(self, string):
		return self.__commandTemplate(string, "7z a %(outfile)s %(infile)s >/dev/null")
	
	def decompress(self, string):
		return self.__commandTemplate(string, "7z x -so %(infile)s > %(outfile)s 2>/dev/null")
